<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.user.id }}
        </template>
      </el-table-column>
      <el-table-column label="实验室" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.lab.name }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="110" align="center">
        <template slot-scope="scope">
            <span v-if="scope.row.lab.rid==1">空闲</span>
            <span v-else-if="scope.row.lab.rid==2">审核中</span>
            <span v-else-if="scope.row.lab.rid==3">使用中</span>
        </template>
      </el-table-column>
      <el-table-column label="使用器材">
        <template>
            <span v-for="lis in this.materials[this.i].materials" :key="lis.id">
              {{lis.name}}
            </span>
        </template>
      </el-table-column>
      <el-table-column label="使用人" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.user.name }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="开始时间" width="200" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.starttime }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="结束时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.endtime }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getList } from '@/api/record'
import { getMaterial } from '@/api/record'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      materials:null,
      listLoading: true,
      i:1
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        this.list = response.data.items
        this.listLoading = false
      })
      getMaterial().then(response => {
        this.materials = response.data.items
        console.log(this.materials)
        this.listLoading = false
      })
    },
    random(lower, upper) {
	    return Math.floor(Math.random() * (upper - lower+1)) + lower;
    }
  }
}
</script>
